﻿using BaseUtils.Logger;
using Microsoft.Web.WebView2.Core;
using Service.src;
using Service.src.NW.Model;
using Service.src.NW.UI;
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Service
{
    public partial class NWStudioHomeForm : Form
    {
        public WebView2Handler ViewHandler = null;
        public static NWUIGeneratorForm nWUIGeneratorForm = null;
        public NWStudioHomeForm()
        {
            InitializeComponent();


            Icon icon = new Icon("logo.ico");
            this.Icon = icon;
            AutoFlowHome.Dock = DockStyle.Fill;
        }

        private void AutoFlowHomeForm_Load(object sender, EventArgs e)
        {
            ViewHandler = new WebView2Handler(AutoFlowHome);
            string r = WebView2Handler.Env ? WebView2Handler.DefaultUrlProd : WebView2Handler.DefaultUrlDev;
            ViewHandler.InitWebView2(r);
        }

        private void AutoFlowHome_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
        {
            ViewHandler.uri = "";
            ViewHandler.MainView_NavigationCompleted(sender, e, AutoFlowHome);
        }

        /// <summary>
        /// 窗口关闭逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NWStudioHomeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult c = MessageBox.Show("是否退出程序", "警告", MessageBoxButtons.OKCancel);
            if (c == DialogResult.OK)
            {
                Task.Run(() =>
                {
                    Program.ExecuteBat("\\nginx-1.24.0\\stop.bat");
                });
                AppData.AppRunSuccess = false;
                // 关闭应用程序
                // Application.Exit();
                if (AutoFlowHome != null)
                {
                    AutoFlowHome.Dispose();
                    AutoFlowHome = null;
                }
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            else
            {
                e.Cancel = true;
            }

        }

        private void AutoFlowHome_LocationChanged(object sender, EventArgs e)
        {
            // LoggerService.Error("页面崩溃了AutoFlowHome_LocationChanged");
        }

        private void AutoFlowHome_ChangeUICues(object sender, UICuesEventArgs e)
        {
            LoggerService.Error("页面崩溃了AutoFlowHome_LocationChanged");
        }
    }
}
